package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import service.DiagnosticianService;
import service.FiebreService;

import command.LitiasisCommand;

public class Sintoma2Controller extends SimpleFormController {

	private DiagnosticianService diagnosticianService;
	private FiebreService fiebreService;
	
	public Sintoma2Controller() {
		this.setCommandClass(LitiasisCommand.class);

	}
	
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		return createCommand();
	}

	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		response.setHeader("Cache-Control","no-cache");
		
		LitiasisCommand litiasisCommand = (LitiasisCommand) command;
		
		HttpSession session = request.getSession();
		LitiasisCommand commandInSession = (LitiasisCommand) session.getAttribute("litiasisCommand");

		String idFiebre = litiasisCommand.getIdFiebre();
		commandInSession.setFiebre(fiebreService.getById(idFiebre));
		commandInSession.setVomito(litiasisCommand.isVomito());
		commandInSession.setEscalofrios(litiasisCommand.isEscalofrios());
		commandInSession.setShockHipobolemico(litiasisCommand.isShockHipobolemico());
		
		session.setAttribute("litiasisCommand", commandInSession);

		List<String> diagnosticos = diagnosticianService.diagnose(commandInSession, DiagnosticianService.RULES_2);		

		ModelAndView modelAndView = super.onSubmit(request, response, command, errors);
		modelAndView.addAllObjects(referenceData(request, command, errors));
		modelAndView.addObject("diagnosticos", diagnosticos);
		return modelAndView;
	}

	public void setDiagnosticianService(DiagnosticianService diagnosticianService) {
		this.diagnosticianService = diagnosticianService;
	}

	public void setFiebreService(FiebreService fiebreService) {
		this.fiebreService = fiebreService;
	}
}
